import random
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.views import APIView
from django import forms
from store.models import Store


class StoreForm(forms.Form):
    id = forms.CharField(label='id',required=False)
    storename = forms.CharField(label='name',required=False)
    ownername = forms.CharField(label='owner',required=False)
    sdescription = forms.CharField(label='des',required=False)

class storehome(APIView):
    def get(self,request):
        return render(request, 'storehome.html', {})

class StoreManage(APIView):
    def get(self, request):
        return render(request, 'store.html', {})
    def post(self,request):
        action = request.POST.get('action')
        if action == '1':
            form = StoreForm(request.POST)
            if form.is_valid():
                storename = form.cleaned_data['storename']
                ownername = form.cleaned_data['ownername']
                sdescription = form.cleaned_data['sdescription']
                storeid = random.randint(0, 100000)
                store=Store.objects.create(storeid=storeid,storename=storename,sdescription=sdescription,ownername=ownername)
                return HttpResponse("新增店铺")
            return HttpResponse("请填写店铺名称")
        elif action == '2':
            # 执行删除操作的逻辑
            form = StoreForm(request.POST)
            if form.is_valid():
                id = form.cleaned_data['id']
                if id:
                    try:
                        store = Store.objects.get(storeid=id)
                        store=store.delete()
                        return HttpResponse("删除成功")
                    except:
                        return HttpResponse("啊欧~店铺不存在哦~~~~")
                return HttpResponse("请输入店铺id！")
            return HttpResponse("提交表单失败！")
        elif action == '3':
            # 执行修改操作的逻辑
            form = StoreForm(request.POST)
            if form.is_valid():
                id = form.cleaned_data['id']
                storename = form.cleaned_data['storename']
                ownername = form.cleaned_data['ownername']
                sdescription = form.cleaned_data['sdescription']
                store = Store.objects.get(storeid=id)
                store.storename=storename
                store.sdescription=sdescription
                store.ownername=ownername
                return HttpResponse("店铺")
            return HttpResponse("提交表单失败！")
        elif action == '4':
            form = StoreForm(request.POST)
            store = Store.objects.all()
            backend_message=[]
            for i in store:
                backend_message.append({
                    'id':i.storeid,
                    'storename':i.storename,
                    'sdescription':i.sdescription,
                    'ownername':i.ownername
                })
            # 执行查询操作的逻辑
            return render(request, 'store.html', {'backend_message': backend_message,'form':form})
        else:
            return HttpResponse("未知操作")

